<template>
  <!-- 新增部门的弹层 -->
  <el-dialog :title="showTitle" :visible="showDialog" @close="cancel">
    <!-- 表单组件  el-form   label-width设置label的宽度   -->
    <!-- 匿名插槽 -->
    <el-form
      ref="deptForm"
      :model="formData"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item label="部门名称" prop="name">
        <el-input
          v-model="formData.name"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input
          v-model="formData.code"
          style="width: 80%"
          placeholder="1-50个字符"
        />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select
          @focus="getEmployeeSimple"
          v-model="formData.manager"
          style="width: 80%"
          placeholder="请选择"
        >
          <!-- 需要循环生成选项   这里做一下简单的处理 显示的是用户名 存的也是用户名-->
          <el-option
            v-for="item in peoples"
            :key="item.id"
            :label="item.username"
            :value="item.username"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input
          v-model="formData.introduce"
          style="width: 80%"
          placeholder="1-300个字符"
          type="textarea"
          :rows="3"
        />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="6">
        <el-button @click="submit" type="primary" size="small">确定</el-button>
        <el-button @click="cancel" size="small">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import {
  addDepartments,
  getDepartments,
  getDepartDetail,
  updateDepartments,
} from "@/api/departments";
import { getEmployeeSimple } from "@/api/employees";
export default {
  props: {
    //用来通知窗体是否显示或者隐藏
    showDialog: {
      type: Boolean,
      default: false,
    },
    //操作当前的节点
    treeNode: {
      type: Object,
      default: null,
    },
    currentObj: {},
  },
  data() {
    return {
      peoples: [], // 接收获取的员工简单列表的数据
      //定义表单数据
      formData: {
        name: "", //部门名称
        code: "", //部门编码
        manager: "", //部门管理者
        introduce: "", //部门介绍
      },
      //定义校检规则
      rules: {
        name: [
          { required: true, message: "部门名称不能为空", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "部门名称要求1-50个字符串",
            trigger: "blur",
          },
          {
            trigger: "blur",
            validator: this.checkNameRepeat, // 自定义函数的形式校验
          },
        ],
        code: [
          { required: true, message: "部门编码不能为空", trigger: "blur" },
          {
            min: 1,
            max: 50,
            message: "部门编码要求1-50个字符",
            trigger: "blur",
          },
          { trigger: "blur", validator: this.checkCodeRepeat },
        ],
        manager: [
          { required: true, message: "负责人不能为空", trigger: "blur" },
        ],
        introduce: [
          { required: true, message: "部门介绍不能为空", trigger: "blur" },
          {
            min: 1,
            max: 300,
            message: "部门编码要求1-50个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  computed: {
    showTitle() {
      return !this.formData.id ? "新增部门" : "编辑部门";
    },
  },
  methods: {
    //判断部门 name 是否重复
    async checkNameRepeat(rules, value, callback) {
      //获取得到所有的数据
      const { depts } = await getDepartments();
      let isRepeat = false;
      //判断是否带有id 带有id就是编辑功能
      if (this.formData.id) {
        isRepeat = depts
          .filter(
            (item) =>
              item.id !== this.formData.id && item.pid === this.treeNode.pid
          )
          .some((item) => item.name === value);
      } else {
        //没有id 就是新增
        isRepeat = depts
          .filter((item) => item.pid === this.treeNode.id)
          .some((item) => item.name === value);
      }
      isRepeat
        ? callback(new Error(`同级部门下已经有${value}的部门了`))
        : callback();
    },
    //检查部门编码是否重复
    async checkCodeRepeat(rules, value, callback) {
      const { depts } = await getDepartments();
      let isRepeat = false;
      //判断是编辑还是新增
      //编辑
      if (this.formData.id) {
        isRepeat = depts.some(
          (item) => item.id !== this.formData.id && item.code === value && value
        );
      } else {
        //新增
        isRepeat = depts.some((item) => item.code === value && value); //value不能为空
      }
      isRepeat
        ? callback(new Error(`组织架构中已有部门使用${value}编码`))
        : callback();
    },
    //获取员工简单列表数据
    async getEmployeeSimple() {
      this.peoples = await getEmployeeSimple();
    },
    //点击确定触发事件
    submit() {
      //进行表单的全局验证
      this.$refs.deptForm.validate(async (res) => {
        if (res) {
          //判断是编辑还是新增
          if (this.formData.id) {
            await updateDepartments(this.formData);
          } else {
            //  await addDepartments({ ...this.formData, pid: this.treeNode.id }); //调用新增接口
            await addDepartments({
              ...this.formData,
              pid: this.treeNode.pid === "-1" ? "" : this.treeNode.id,
            }); //调用新增接口
          }
          this.$emit("refreshdata"); //传给父组件 新增数据成功 重新拉取数据
          console.log("22");
          this.$message({
            type: "success",
            message: "操作成功",
          });
          this.$emit("update:showDialog", false);
          this.formData = {
            name: "",
            code: "",
            manager: "",
            introduce: "",
          };
        }
      });
    },
    cancel() {
      this.$emit("update:showDialog", false);
      //重置校验
      this.$refs.deptForm.resetFields(); //
      // this.formData = {
      //   name: "",
      //   code: "",
      //   manager: "",
      //   introduce: "",
      // };
      console.log("结束");
    },
    async getDepartDetail(id) {
      this.formData = await getDepartDetail(id);
    },
  },
  created() {
    this.getEmployeeSimple();
  },
};
</script>

<style lang="scss" scoped>
</style>
